package com.itheima.customer.web.action;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.itheima.customer.domain.BaseDict;
import com.itheima.customer.domain.Customer;
import com.itheima.customer.service.ICustomerService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

@Controller("customerAction")
@Scope("prototype")
@ParentPackage("struts-default")
@Namespace("/customer")
public class customerAction extends ActionSupport implements ModelDriven<Customer> {

	private Customer customer = new Customer();

	@Autowired
	private ICustomerService customerService;

	private List<Customer> customers = null;

	private List<BaseDict> custSources;

	private List<BaseDict> custLevels;
	
	private String custSourceId;
	
	private String custLevelId;
	
	public List<BaseDict> getCustSources() {
		return custSources;
	}

	public void setCustSources(List<BaseDict> custSources) {
		this.custSources = custSources;
	}

	public List<BaseDict> getCustLevels() {
		return custLevels;
	}

	public void setCustLevels(List<BaseDict> custLevels) {
		this.custLevels = custLevels;
	}

	public String getCustSourceId() {
		return custSourceId;
	}

	public void setCustSourceId(String custSourceId) {
		this.custSourceId = custSourceId;
	}

	public String getCustLevelId() {
		return custLevelId;
	}

	public void setCustLevelId(String custLevelId) {
		this.custLevelId = custLevelId;
	}


	
	public List<Customer> getCustomers() {
		return customers;
	}

	public void setCustomers(List<Customer> customers) {
		this.customers = customers;
	}

	@Override
	public Customer getModel() {
		return customer;
	}
	
	/**
	 * 加载添加客户页面
	 * @return
	 */
	@Action(value="addCustomerUI",results={
			@Result(name="addCustomerUI",type="dispatcher",location="/jsp/customer/add.jsp")
	})
	public String addCustomerUI(){
		
		custSources = customerService.findCustSources();
		custLevels  = customerService.findCustLevels();
		
		return "addCustomerUI";
	}
	
	/**
	 * 查询所有客户
	 * @return
	 */
	@Action(value="listCustomer",results={
			@Result(name="listCustomer",location="/jsp/customer/list.jsp")
	})
	public String listCustomer(){
		
		DetachedCriteria dCriteria = DetachedCriteria.forClass(Customer.class);
		//拼接查询条件
		
		if(StringUtils.isNotBlank(customer.getCustName())){
			dCriteria.add(Restrictions.like("custName","%"+customer.getCustName()+"%" ));
		}
		if(StringUtils.isNotBlank(customer.getCustIndustry())){
			dCriteria.add(Restrictions.like("custIndustry","%"+customer.getCustIndustry()+"%" ));
			
		}
		
		if(StringUtils.isNotBlank(custSourceId)){
			
			dCriteria.add(Restrictions.eq("custSourceBaseDict.dictId", custSourceId));
		}
		
		if(StringUtils.isNotBlank(custLevelId)){
			dCriteria.add(Restrictions.eq("custLevelBaseDict.dictId", custLevelId));
		}
		
		 customers  = customerService.findAllCustomer(dCriteria);
		 
		custSources = customerService.findCustSources();
		custLevels  = customerService.findCustLevels();
		 
		return "listCustomer";
	}
	
	/**
	 * 添加客户
	 * @return
	 */
	@Action(value="addCustomer",results={
			@Result(name="success",type="redirect",location="/jsp/success.jsp")
	})
	public String addCustomer(){
		//1.根据id查询客户来源和客户级别
		BaseDict custSource = customerService.findBaseDictById(custSourceId);
		BaseDict custLevel = customerService.findBaseDictById(custLevelId);
		//2.建立客户来源和客户的关联关系
		customer.setCustSourceBaseDict(custSource);
		//3.建立客户级别和客户的关联关系
		customer.setCustLevelBaseDict(custLevel);
		//4.保存客户
		customerService.addCustomer(customer);
		return SUCCESS;
	}
	
	/**
	 * 加载修改页面
	 */
	@Action(value="editCustomerUI",results={
			@Result(name="editCustomerUI",type="dispatcher",location="/jsp/customer/edit.jsp")
	})
	public String editCustomerUI(){
		Customer c = new Customer();
		c = customerService.findCustomer(customer);
//		ServletActionContext.getRequest().setAttribute("customer", customer);
		ActionContext.getContext().getValueStack().push(c);
		 
		custSources = customerService.findCustSources();
		custLevels  = customerService.findCustLevels();
		return "editCustomerUI";
	}
	
	/**
	 * 修改客户信息
	 * @return
	 */
	@Action(value="editCustomer",results={
			@Result(name="success",type="redirect",location="/jsp/success.jsp")
	})
	public String editCustomer(){
		//1.根据id查询客户来源和客户级别
				BaseDict custSource = customerService.findBaseDictById(custSourceId);
				BaseDict custLevel = customerService.findBaseDictById(custLevelId);
				//2.建立客户来源和客户的关联关系
				customer.setCustSourceBaseDict(custSource);
				//3.建立客户级别和客户的关联关系
				customer.setCustLevelBaseDict(custLevel);
				//4.保存客户
		
		customerService.update(customer);
		return SUCCESS;
	}
	
	
	/**
	 * 删除客户
	 * @return
	 */
	@Action(value="removeCustomer",results={
			@Result(name="success",type="redirect",location="/jsp/success.jsp")
	})
	public String removeCustomer(){
		customerService.delete(customer);
		return SUCCESS;
	}
	
}
